package com.md.common.utils.bizid;

import com.md.common.utils.date.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.time.LocalDateTime;
import java.util.Random;
import java.util.UUID;

/**
 * 功能描述：业务ID生成器
 *
 * @author lujianping@oak168.com
 * @version 1.0
 * @date 2018-08-01 16:50
 * @since JDK 1.8
 */
public class BizIdGenerator {
    private static final Logger LOGGER = LoggerFactory.getLogger(BizIdGenerator.class);
    private static String machineId;//集群机器id，手动配置
    private static final int RANDOM_SIZE = 5;//随机数的长度
    private static final int MAX = 100000;

    private BizIdGenerator() {
    }

    /**
     * 加载ID生成器配置
     *
     * @param machineNo 机器号，集群部署中指定的机器号
     */
    public static void loadConfig(String machineNo) {
        machineId = machineNo;
    }

//    static {
//        PropertyUtils.loadProperties("/conf/disconf.properties");
//        machineId = PropertyUtils.getStringValue("machine.id") == null ? "1" : PropertyUtils.getStringValue("machine.id");
//    }

    /**
     * 业务ID的组成. <br/>
     * 业务ID代码 - 1到2位
     * +所在集群机器id - 1位，需要手动修改配置
     * +yyyyMMddHHmm - 12位
     * +UUID.hashcode - 后5位
     *
     * @param bizIdType
     * @return
     */
    public static synchronized String nextId(BizIdTypeEnum bizIdType) {
        String id;
        int hashCode = UUID.randomUUID().toString().hashCode();
        if (hashCode < 0) {
            hashCode = -hashCode;
        }
        String hashCodeStr = hashCode + "";
        StringBuilder builder = new StringBuilder();
        builder.append(bizIdType.getPrefix())
                .append(machineId)
                .append(DateUtil.formaterLocalDateTime(LocalDateTime.now(), DateUtil.SHORT_YMDHM))
                .append(hashCodeStr.substring(hashCodeStr.length() - 5));
        id = builder.toString();
        LOGGER.info("bizIdType={}, id={}", bizIdType, id);
        return id;
    }

    /**
     * 普通标识的组成. <br/>
     * 所在集群机器id - 1位，需要手动修改配置
     * +id类型 - 1位
     * +中间5位随机数 - 5位
     * +UUID.hashcode - 10位
     *
     * @param bizIdType
     * @return
     */
    public static synchronized String createId(BizIdTypeEnum bizIdType) {
        String id;
        int hashCode = UUID.randomUUID().toString().hashCode();
        if (hashCode < 0) {
            hashCode = -hashCode;
        }
        StringBuilder builder = new StringBuilder();
        builder.append(machineId)
                .append(bizIdType.getValue())
                .append(getRandom())
                .append(String.format("%010d", hashCode));
        id = builder.toString();
        LOGGER.info("bizIdType={}, id={}", bizIdType, id);
        return id;
    }

    private static String getRandom() {
        Random rad = new Random();
        String result = String.valueOf(rad.nextInt(MAX));
        if (result.length() != RANDOM_SIZE) {
            return getRandom();
        }
        return result;
    }

    public static void main(String[] arg) {
        machineId = "TPM";
        System.out.print(nextId(BizIdTypeEnum.THIRD_PAY_MEMBER_ID));
    }
}
